package net.hcpme.activity;

import org.json.JSONException;
import org.json.JSONObject;

import net.hcpme.service.ReceiveMessageService;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Login extends BaseActivity implements OnClickListener {

    public static final String LOGIN_MESSAGE_RECEIVE_BROADCAST = "login_message_receive_broadcast";

    private EditText userNameEt;
    private EditText passwordEt;
    private Spinner selectRoomSp;
    private ArrayAdapter<CharSequence> adapter;
    private Button loginBtn;

    private Handler handler = new Handler();

    private ProgressDialog pdialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        userNameEt = (EditText) findViewById(R.id.login_user_name_et);
        passwordEt = (EditText) findViewById(R.id.login_password_et);
        selectRoomSp = (Spinner) findViewById(R.id.login_select_room_sp);
        adapter = ArrayAdapter.createFromResource(this, R.array.room, android.R.layout.simple_spinner_item);
        // 设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 将adapter2 添加到spinner中
        selectRoomSp.setAdapter(adapter);
        // 添加事件Spinner事件监听
        // selectRoomSp.setOnItemSelectedListener(new
        // SpinnerXMLSelectedListener());
        // 设置默认值
        selectRoomSp.setVisibility(View.VISIBLE);

        loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);

        registerBoradcastReceiver();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.login_btn:
            if (!checkInput()) {
                return;
            }
            showProgressDialog();
            JSONObject userInfo = getLoginJson(userNameEt.getText().toString(), passwordEt.getText().toString());
            // 创建Intent
            Intent intent = new Intent(this, ReceiveMessageService.class);
            intent.putExtra("message", userInfo.toString());
            startService(intent);
            break;

        default:
            break;
        }
    }

    private void showProgressDialog() {
        pdialog = new ProgressDialog(this, 0);
        pdialog.setTitle("登录中...");
        pdialog.setCancelable(true);
        pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pdialog.show();
    }

    private boolean checkInput() {
        String userName = userNameEt.getText().toString();
        String password = passwordEt.getText().toString();
        if (userName == null || "" == userName || "".equals(userName)) {
            Toast.makeText(this, R.string.user_name_prompt, Toast.LENGTH_LONG).show();
            return false;
        }
        if (password == null || "" == password || "".equals(password)) {
            Toast.makeText(this, R.string.password_prompt, Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    // 组装json
    private JSONObject getLoginJson(String userName, String password) {
        // 首先最外层是{}，是创建一个对象
        JSONObject user = new JSONObject();
        try {
            JSONObject accress = new JSONObject();
            accress.put("帐户", userName);
            accress.put("密码", password);
            accress.put("验证码", "");
            JSONObject version = new JSONObject();
            version.put("版本", 1.0);
            version.put("会员", new JSONObject().put("登录", accress));
            user.put("文档", version);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(LOGIN_MESSAGE_RECEIVE_BROADCAST)) {
                String re = intent.getStringExtra("data");
                Log.d("tag~~~", ">>>-------" + intent.getStringExtra("data"));
                if ("success".equals(re)) {
                    Login.this.startActivity(new Intent(Login.this, HomeActivity.class));
                    Login.this.finish();
                } else {

                }
            }
        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(LOGIN_MESSAGE_RECEIVE_BROADCAST);
        // 注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

}
